// @vue/component
import {catList} from "@/api/goodsCat";
import fi from "element-ui/src/locale/lang/fi";
import {goodsCreate, goodsDelete, goodsList, goodsStockOperation, goodsStockSave, goodsUpdate} from "@/api/goods";
import Pagination from "@/components/Pagination/index.vue";
import waves from "@/directive/waves";

export default {
  directives: {waves}, filters: {
    dayPerStatus(status) {
      const statusMap = {
        1: 'success', 2: 'info', 0: 'danger'
      }
      return statusMap[status]
    }, dayPerFilter(dayPer) {
      if(dayPer===0){
        return "不限";
      }else{
        return  "每日 " + dayPer + " 件"
      }

    }, statusText(status) {
      const statusMapText  = {
        1: '启用', 0: '停用'
      }
      console.log(status)
      return statusMapText[status]
    }
  },
  name: 'Goods',

  components: {Pagination},

  mixins: [],

  props: {},

  data () {

    return {
      base_url:process.env.VUE_APP_BASE_DOMAIN,
      goodsQuery:{
        goodsName:'',
        goodsCat:'',
        page_num:1,
        page_size:10
      },
      goodsTotal:0,
      goodsList:[],
      listLoading:false,
      goodsAddVisible:false,
      goodsEditShow:false,
      goodsStockVisible:false,
      stockOperationVisible:false,
      goodsCatList:[],
      goodsInfo:{
        name:"",
        goods_cat:[],
        goods_desc:'',
        image:''
      },
      goodsTempInfo:{
        id : '',
        name:"",
        cat_id:[],
        goods_desc:'',
        image:''
      },
      initStock:false,
      goodsSetInfo:{
        id:'',
        total_num:0,
        use_num:0,
        leave_num:0,
        warn_num:0,
        day_per:0,
      },
      stockOperationForm:{
        title:'',
        max_value:0,
        placeholder:'0',
        field_label:'0',
      },
      stockInfo:{
        goods_id:'',
        total_num:0,
        goods_action:''
      },
      fileList:[],
      uploadHeaders:{   "Authorization":'Bearer ' + localStorage.getItem('Access-Token')  }
    }
  },

  computed: {

  },

  watch: {
    'goodsSetInfo.total_num': {
      handler(newName, oldName) {
        // ...
        if(this.goodsSetInfo.total_num<this.goodsSetInfo.use_num){
          this.$notify({
            title: '错误', message: '总数不能小于领用数量', type: 'error', duration: 2000
          })
          return false
        }
        this.goodsSetInfo.leave_num = this.goodsSetInfo.total_num-this.goodsSetInfo.use_num
      },
      deep: true,
      immediate: true
    },
    'goodsSetInfo.use_num': {
      handler(newName, oldName) {
        let leave_num =  this.goodsSetInfo.total_num-this.goodsSetInfo.use_num
        if(leave_num<0){

          this.$notify({
            title: '错误', message: '领用数量不能大于总数量', type: 'error', duration: 2000
          })
          return false
        }
        this.goodsSetInfo.leave_num = leave_num

      },
      deep: true,
      immediate: true
    },
    'stockInfo.total_num': {
      handler(newName, oldName) {
        console.log(this.stockOperationForm.max_value)
        if(this.stockOperationForm.field_label.indexOf("进货") === -1){
          if(this.stockInfo.total_num>this.stockOperationForm.max_value){
            this.$notify({
              title: '错误', message: this.stockOperationForm.field_label + '不能大于剩余可用数量', type: 'error', duration: 2000
            })
            this.stockInfo.total_num = this.stockOperationForm.max_value
            return false
          }
        }

      },
      deep: true,
      immediate: true
    },
  },

  created () {
    this.getGoodsList()
    this.getGoodsCat()
  },

  methods: {
    getGoodsList(){
      this.listLoading = true
      goodsList(this.goodsQuery).then(res=>{
        if(res.code===200){
          this.listLoading = false
          this.goodsTotal = res.data.page_info.count
          this.goodsList = res.data.data
        }
        console.log(res)
      })
    },

    getGoodsCat() {
      catList().then(res=>{
        if(res.code===200){
          this.goodsCatList = res.data
        }
      })
    },

    goodsAdd(){
      goodsCreate(this.goodsInfo).then(res=>{
        if (res.code === 200) {
          this.goodsAddVisible = false
          this.goodsInfo = {}
          this.fileList=[];
          this.$notify({
            title: '成功', message: '商品添加成功', type: 'success', duration: 2000
          })
          this.getGoodsList();
        } else {
          this.$notify({
            title: '失败', message: res.message, type: 'error', duration: 2000
          })
        }
      })
    },

    handleRemove(file, fileList) {
      this.goodsInfo.goods_image = ''
      console.log(file, fileList);
    },

    handlePreview(file) {
      console.log(file);
    },
    handeSuccess(response, file, fileList){
      this.goodsInfo.goods_image = response.data.file_path
    },
    handleUpdate(row){
      console.log(row)
      this.goodsTempInfo = row
      this.goodsEditShow = true
    },
    handleDelete(row,index){

      goodsDelete(row.id).then(res=>{
        if (res.code === 200) {
          this.$notify({
            title: '成功', message: '商品删除成功', type: 'success', duration: 2000
          })
          this.getGoodsList();
        } else {
          this.$notify({
            title: '失败', message: res.message, type: 'error', duration: 2000
          })
        }
      })
    },

    goodsSetting(row){

      this.goodsStockVisible = true
      this.goodsSetInfo.id = row.id
      if(row.goods_stock){
        this.initStock = true
      }
      this.goodsSetInfo.total_num = row.goods_stock?row.goods_stock.total_num:0
      this.goodsSetInfo.use_num = row.goods_stock?row.goods_stock.use_num:0
      this.goodsSetInfo.leave_num = row.goods_stock?row.goods_stock.leave_num:0
      this.goodsSetInfo.warn_num = row.goods_stock?row.goods_stock.warn_num:0
      this.goodsSetInfo.day_per = row.day_per
    },
    goodsSetSave(){
      console.log("商品配置保存")
      console.log(this.goodsSetInfo)
      goodsStockSave(this.goodsSetInfo.id,this.goodsSetInfo).then(res=>{
        if (res.code === 200) {
          this.goodsStockVisible = false
          this.$notify({
            title: '成功', message: '商品库存配置成功', type: 'success', duration: 2000
          })
          this.getGoodsList();
        } else {
          this.$notify({
            title: '失败', message: res.message, type: 'error', duration: 2000
          })
        }
      })
    },
    handleStock(row,type){
      this.stockOperationForm = {}
      this.stockInfo = {}
      this.stockOperationForm.title = '商品'+type;
      this.stockOperationForm.field_label = type+'数量';
      this.stockOperationForm.placeholder = "请输入"+type+'数量';
      this.stockOperationForm.max_value = row.goods_stock.total_num-row.goods_stock.use_num-row.goods_stock.bad_num
      this.stockInfo.goods_id = row.id
      this.stockInfo.goods_action = type
      this.stockOperationVisible = true;

    },
    stockSave(){
      goodsStockOperation(this.stockInfo.goods_id,this.stockInfo).then(res=>{
        if (res.code === 200) {
          this.stockOperationVisible = false
          this.$notify({
            title: '成功', message: '商品'+this.stockInfo.goods_action+'成功', type: 'success', duration: 2000
          })
          this.getGoodsList();
        } else {
          this.$notify({
            title: '失败', message: res.message, type: 'error', duration: 2000
          })
        }
      })
    },
    stockHistory(goods_id){

    },
    handeEditSuccess(response, file, fileList){
      this.goodsTempInfo.image = response.data.file_path

    },
    goodsEdit(){
      goodsUpdate(this.goodsTempInfo.id,this.goodsTempInfo).then(res=>{
        if (res.code === 200) {
          this.goodsEditShow = false
          this.goodsTempInfo = {}
          this.$notify({
            title: '成功', message: '商品编辑成功', type: 'success', duration: 2000
          })
          this.fileList=[];
          this.getGoodsList();
        } else {
          this.$notify({
            title: '失败', message: res.message, type: 'error', duration: 2000
          })
        }
      })
    }


  }
}
